//
//  WSAnswerTableCell.m
//  WisdomStar
//
//  Created by 谭飞 on 2018/10/24.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "WSAnswerTableCell.h"

#import "QuestionInfo.h"

@interface WSAnswerTableCell ()
@property (weak, nonatomic) IBOutlet UIView *imageContentView;
@property (weak, nonatomic) IBOutlet UIImageView *eyeIcon;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (strong, nonatomic) UIStackView *stackView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageViewHeight;

@property (nonatomic, strong) NSMutableArray *saveImages;

@end

@implementation WSAnswerTableCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - Setter

- (void)setQuestions:(QuestionInfo *)questions {
    _questions = questions;
    self.titleLabel.text = questions.title;
    self.countLabel.text = [NSString stringWithFormat:@"(%@)",@(questions.view)];
    self.timeLabel.text = questions.push_time;
    
    self.imageViewHeight.constant = questions.images.count ? 92.f : 0;
    [self layoutIfNeeded];
    
    NSMutableArray *subViews = [NSMutableArray arrayWithCapacity:questions.images.count];
    [questions.images enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIImageView *imageView = [UIImageView new];
        [imageView imageWithCompleteUrl:obj defaultImage:kPlaceHolderImage];
        [subViews addObject:imageView];
    }];
    if (subViews.count && subViews.count<3) {
        [subViews addObject:[UIImageView new]];
    }
    [self addStackView:subViews];
}

#pragma mark - Getter

- (NSMutableArray *)saveImages {
    if (_saveImages == nil) {
        _saveImages = [NSMutableArray array];
    }
    return _saveImages;
}


// add subviews
- (void)addStackView:(NSArray<__kindof UIView *> *)subviews {
    [self.stackView removeFromSuperview]; // 移除之前的视图
    self.stackView = [[UIStackView alloc] initWithArrangedSubviews:subviews];
    self.stackView.axis = UILayoutConstraintAxisHorizontal;
    self.stackView.distribution = UIStackViewDistributionFillEqually;
    self.stackView.spacing = 10.f;
    self.stackView.alignment = UIStackViewAlignmentFill;
    [self.imageContentView addSubview:self.stackView];
    [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.imageContentView);
    }];
}


@end
